##############################
# Analysis of Excess Returns, Disposition Effects, and Peak Timing in Stock Sales
# --------------------------------------------------
#
# This script uses data from "MAXC_peak_5update.csv" and focuses on the sell day sample (`use_days == "sell"`). 
# Peaks are defined as the highest prices since purchase.
#
# Part 1: Calculating Excess Returns
# ----------------------------------
# The first part of the script calculates excess post-sale returns relative to the FTSE100. 
# The returns are analyzed over three periods:
# 1. One month after the sale
# 2. 166 days after the sale (the average holding period)
# 3. One year after the sale
#
# These calculations are applied to both realized gains and unrealized losses, providing an estimate 
# of the economic impact of the disposition effect and the peak price effect.
#
# Output:
# -------
# The results are saved in various .tex files within the "tables" directory, which contribute to the following tables in the paper:
#
# - **Table A43: Average Returns**
#   - `tables/av_returns.tex`
#   - `tables/av_returns_peak.tex`
# 
# - **Table 11: Ex Post Returns for Stocks and Housing**
#   - `tables/excess_returns.tex`
#   - `tables/excess_returns_peak.tex`
#
# - **Table A44: Average Returns by Frequency of Peaks**
#   - `tables/av_returns_low.tex`
#   - `tables/av_returns_peak_low.tex`
#   - `tables/av_returns_high.tex`
#   - `tables/av_returns_peak_high.tex`
#
# Part 2: Additional Analysis
# ---------------------------
# The script also explores the impact of asset volatility, comparing highly volatile assets 
# with an above-average number of peaks to those with lower volatility.
#
# - **Table A45: Ex Post Returns by Frequency of Peaks**
#   - `tables/excess_returns_low.tex`
#   - `tables/excess_returns_peak_low.tex`
#   - `tables/excess_returns_high.tex`
#   - `tables/excess_returns_peak_high.tex`
#
#
# Part 3: Robustness Checks
# -------------------------
#
# The second part of the script conducts robustness. First, it runs an analysis of the purchase and peak price disposition effects, splitting the sample into ten deciles 
#     based on returns since purchase.
# - **Table 6: Disposition Effects by Decile**
#   - `tables/clean_OLS_deciles_sellsample.tex`
#
# Second, it checks investor responses to a wide range of gain measures, including:
# - Gain since purchase
# - Gain since peak
# - Gain since yesterday
# - Gain since the past week
# - Gain since the past month
# - Gain since the past quarter
# - Year-to-date gain (i.e., gain from the start of the current year to the present date)
#
# - **Table 8: Controlling for Alternative Reference Points**
#   - `tables/controling_alternative_rp_sell_sample.tex`
#
# Part 4: Timing of Peaks and Decision-Making
# -------------------------------------------
# Finally, the script examines how the timing of peaks influences selling decisions. 
# Sales are categorized based on the recency of the last peak, occurring in the current month, 
# the previous month, two months ago, three months ago, four months ago.
# and five or more months ago.
#
# - **Table 9: Impact of Peak Timing**
#   - `tables/peak_timing_wide_sell_sample.tex`
##############################





if(data_name_read=="MAXC_peak_5update.csv" & use_days=="sell"){ 

# Part 1: Calculating Excess Returns
# ----------------------------------

# reading prices


dd <- fread("D:/files_moved_19/output datastream/datastream data/310518_prices/datastream_20180601.csv")    

dd[,date:=as.Date(date, "%Y-%m-%d")]
stocks<- unique(data_for_peaks$Code_used_DS)
dd_for_count_peaks<- dd[Code_used_DS %in% stocks][,.(app,   date, Code_used_DS)]
dd<-dd[Code_used_DS %in% stocks,.(app, date ,Code_used_DS )][order(Code_used_DS,date)]

dd[ , date_lead21:=shift(date,21,NA,"lead") ,by=.(Code_used_DS)] 
dd[ , date_lead260:=shift(date,260,NA,"lead") ,by=.(Code_used_DS)] 

dd[ , app_lead21:=shift(app,21,NA,"lead") ,by=.(Code_used_DS)] 
dd[ , app_lead260:=shift(app,260,NA,"lead") ,by=.(Code_used_DS)] 
dd[ , app_lead120:=shift(app,120,NA,"lead") ,by=.(Code_used_DS)] 

dd[!is.na(app_lead21),return.to.lead21100:= 100*( app_lead21-app )/app ]
dd[!is.na(app_lead260),return.to.lead260100:=100*(app_lead260 -app)/app ]
dd[!is.na(app_lead120),return.to.lead120100:= 100*(app_lead120-app)/app]
dd[,port_date:=date]


library(lubridate)

ftse100h <- fread("D:/files_moved_19/output datastream/datastream data/FTSE 100 Historical Data.csv")

ftse100h[, port_date:=mdy(Date)]
ftse100h<-ftse100h[,.(port_date, `Adj Close**`)][order(port_date)]
dd<-merge(dd, ftse100h, by=c("port_date"), all.x=T)
ftse100h_dates_used<-unique(dd[,.(port_date, `Adj Close**`)])[order(port_date)]
ftse100h_dates_used[, ftse := as.numeric(gsub(",", "", `Adj Close**`))]
ftse100h_dates_used[, ftse:= na.locf(ftse, na.rm = F)]

ftse100h_dates_used[ , ftse_lead21:=shift(ftse,21,NA,"lead") ] 
ftse100h_dates_used[ , ftse_lead260:=shift(ftse,260,NA,"lead")] 
ftse100h_dates_used[ , ftse_lead120:=shift(ftse,120,NA,"lead") ] 

ftse100h_dates_used[!is.na(ftse_lead21),ftsereturn.to.lead21100:= 100*( ftse_lead21-ftse )/ftse ]
ftse100h_dates_used[!is.na(ftse_lead260),ftsereturn.to.lead260100:=100*(ftse_lead260 -ftse)/ftse ]
ftse100h_dates_used[!is.na(ftse_lead120),ftsereturn.to.lead120100:= 100*(ftse_lead120-ftse)/ftse]

dd<-unique(dd[,.(port_date,Code_used_DS, return.to.lead21100, return.to.lead120100 , return.to.lead260100)])
dd<-merge(dd, ftse100h_dates_used[,.(port_date, ftsereturn.to.lead21100, ftsereturn.to.lead120100, ftsereturn.to.lead260100)], by=c("port_date"), all.x=T)

dd[,excess21:=return.to.lead21100- ftsereturn.to.lead21100]
dd[,excess120:=return.to.lead120100- ftsereturn.to.lead120100]
dd[,excess260:=return.to.lead260100- ftsereturn.to.lead260100]

data_for_peaks<- merge( data_for_peaks, dd[,.(Code_used_DS, port_date, excess21, excess120, excess260)], 
                                        by=c("Code_used_DS", "port_date"), all.x=T)

data_for_peaks[gain.since.pur==1 & sell==1,action:="sell winer"]
data_for_peaks[gain.since.pur==0 & sell==0,action:="hold loser"]

data_for_peaks[gain.since.point ==1 & sell==1,action_peak:="sell winer"]
data_for_peaks[gain.since.point ==0 & sell==0,action_peak:="hold loser"]

data_for_peaks[gain.since.pur  ==1 & sell==1,actions_DE:="realized gains"]
data_for_peaks[gain.since.pur  ==1 & sell==0,actions_DE:="paper gains"]

data_for_peaks[gain.since.pur ==0 & sell==0,actions_DE:="paper losses"]
data_for_peaks[gain.since.pur ==0 & sell==1,actions_DE:="realized losses"]

data_for_peaks[gain.since.point   ==1 & sell==1,actions_DE_peak:="realized gains"]
data_for_peaks[gain.since.point   ==1 & sell==0,actions_DE_peak:="paper gains"]

data_for_peaks[gain.since.point ==0 & sell==0,actions_DE_peak:="paper losses"]
data_for_peaks[gain.since.point ==0 & sell==1,actions_DE_peak:="realized losses"]

av_returns<-data_for_peaks[num_sales_the_day>0][, mean(return.since.pur100), by=.(actions_DE)][order(actions_DE)] 
av_returns[, actions_DE := fcase(
  actions_DE == "realized gains", "Realized Returns - Asset Above Purchase Price",
  actions_DE == "paper gains", "Unrealized Returns - Asset Above Purchase Price",
  actions_DE == "paper losses", "Unrealized Returns - Asset Below Purchase Price",
  actions_DE == "realized losses", "Realized Returns - Asset Below Purchase Price"
)]

av_returns[, V1:=round(V1,2)]

options(xtable.sanitize.text.function=NULL)
options(xtable.sanitize.colnames.function = identity)

table <- capture.output(print(xtable(av_returns), include.rownames=FALSE)) 

setwd(wd_tables)
write.table(table[9:12], col.names = F, 
            row.names = F, quote = FALSE, "av_returns.tex")


data_for_peaks[,.N, by=.(gain.since.pur, gain.since.point) ]
av_returns<-data_for_peaks[num_sales_the_day>0][, mean(return.since.pur100), by=.(actions_DE_peak)][order(actions_DE_peak)]
av_returns[, actions_DE_peak := fcase(
  actions_DE_peak == "realized gains", "Realized Returns - Asset Above Peak Price",
  actions_DE_peak == "paper gains", "Unrealized Returns - Asset Above Peak Price",
  actions_DE_peak == "paper losses", "Unrealized Returns - Asset Below Peak Price",
  actions_DE_peak == "realized losses", "Realized Returns - Asset Below Peak Price"
)]

av_returns[, V1:=round(V1,2)]
table <- capture.output(print(xtable(av_returns), include.rownames=FALSE)) 

setwd(wd_tables)
write.table(table[9:12], col.names = F, 
            row.names = F, quote = FALSE, "av_returns_peak.tex")



# Calculating Excess Returns

library(lfe)

summary(e21<- felm(excess21~ action + 0, data_for_peaks[num_sales_the_day>0]))
summary(e21_diff<- felm(excess21~ action, data_for_peaks[num_sales_the_day>0]))

summary(e120<- felm(excess120~ action + 0, data_for_peaks[num_sales_the_day>0]))
summary(e120_diff<- felm(excess120~ action, data_for_peaks[num_sales_the_day>0]))

summary(e260<- felm(excess260~ action + 0, data_for_peaks[num_sales_the_day>0]))
summary(e260_diff<- felm(excess260~ action, data_for_peaks[num_sales_the_day>0]))


library(stargazer)
table<-stargazer( e21, e120,e260,
                  align=TRUE,no.space=TRUE, covariate.labels=c( "Av. Excess Return on Paper Losses","Av. Excess Return on Winning Stocks Sold"),
                  type="latex",
                  omit.table.layout = "n",
                  float=F, 
                  table.layout ="-dc#-tas-",
                  style = "aer",
                  column.sep.width = "10pt",
                  digits=4 , report = "vc"
)

library(stringr)
table <- str_replace_all(table, "\\^", "")

table2<-stargazer( e21_diff, e120_diff,  e260_diff,
                  align=TRUE,no.space=TRUE, covariate.labels=c("Difference in Excess Returns"),
                  type="latex",
                  omit.table.layout = "n",
                  float=F, 
                  table.layout ="-dc#-tas-",
                  style = "aer",
                  column.sep.width = "10pt",
                  digits=4 
)

library(stringr)
table2 <- str_replace_all(table2, "\\^", "")

setwd(wd_tables)
write.table(c(table[c(11,10)],table2[10]), col.names = F, 
            row.names = F, quote = FALSE,  paste0("excess_returns.tex")) 



summary(e21<- felm(excess21~ action_peak + 0, data_for_peaks[num_sales_the_day>0]))
summary(e21_diff<- felm(excess21~ action_peak, data_for_peaks[num_sales_the_day>0]))

summary(e120<- felm(excess120~ action_peak + 0, data_for_peaks[num_sales_the_day>0]))
summary(e120_diff<- felm(excess120~ action_peak, data_for_peaks[num_sales_the_day>0]))

summary(e260<- felm(excess260~ action_peak + 0, data_for_peaks[num_sales_the_day>0]))
summary(e260_diff<- felm(excess260~ action_peak, data_for_peaks[num_sales_the_day>0]))


table<-stargazer( e21, e120,e260,
                  align=TRUE,no.space=TRUE, covariate.labels=c( "Av. Excess Return on Paper Losses","Av. Excess Return on Winning Stocks Sold"),
                  type="latex",
                  omit.table.layout = "n",
                  float=F, 
                  table.layout ="-dc#-tas-",
                  style = "aer",
                  column.sep.width = "10pt",
                  digits=4 , report = "vc"
)

library(stringr)
table <- str_replace_all(table, "\\^", "")

table2<-stargazer( e21_diff, e120_diff,  e260_diff,
                   align=TRUE,no.space=TRUE, covariate.labels=c("Difference in Excess Returns"),
                   type="latex",
                   omit.table.layout = "n",
                   float=F, 
                   table.layout ="-dc#-tas-",
                   style = "aer",
                   column.sep.width = "10pt",
                   digits=4 #, report = "vc"
)

library(stringr)
table2 <- str_replace_all(table2, "\\^", "")

setwd(wd_tables)
write.table(c(table[c(11,10)],table2[10]), col.names = F, 
            row.names = F, quote = FALSE,  paste0("excess_returns_peak.tex")) 


# Part 2: Volatility Analysis
# ---------------------------
# counting peaks
  dd_for_count_peaks[,from_DD:=1]
  setnames(dd_for_count_peaks, "date", "port_date")

  dd_for_count_peaks<- dd_for_count_peaks[port_date>= "2012-04-13" & port_date<="2016-03-29"][order(Code_used_DS, port_date)]
  
  dd_for_count_peaks[ , app_yest:=shift(app,1,NA,"lag") ,by=.(Code_used_DS)] 
  dd_for_count_peaks[ , port_date_yest:=shift(port_date,1,NA,"lag") ,by=.(Code_used_DS)] 
  dd_for_count_peaks[!is.na(app_yest),MaxPriceSoFar:=cummax(app_yest),by=.(Code_used_DS)]
  

  dd_for_count_peaks[MaxPriceSoFar==app_yest & app>= MaxPriceSoFar, MaxPriceSoFar:=NA]
  dd_for_count_peaks[, MaxPriceSoFar := na.locf(MaxPriceSoFar, na.rm = F), by=.(Code_used_DS)] 
  dd_for_count_peaks[, order:=seq_along(port_date), by=.(Code_used_DS, MaxPriceSoFar)]
  dd_for_count_peaks[MaxPriceSoFar==app_yest & order==1, date_app_peak:=port_date_yest]
  dd_for_count_peaks[, date_app_peak := na.locf(date_app_peak, na.rm = F), by=.(Code_used_DS, MaxPriceSoFar)] 
  
  setnames(dd_for_count_peaks, "date_app_peak", "date_past_peak")
  setnames(dd_for_count_peaks, "MaxPriceSoFar", "app_past_peak")
  
  dd_for_count_peaks[, distance_to_peak:=as.numeric(port_date-date_past_peak)]

  dd_for_count_peaks[,pass:=0]
  dd_for_count_peaks[app>app_past_peak, pass:=1]
  dd_for_count_peaks[is.na(app_past_peak), pass:=NA]
  
  setnames(dd_for_count_peaks, "app", "appDS")
  setnames(dd_for_count_peaks, "pass", "pass_peak")
  
  update_days=5
  dd_for_count_peaks[ , app_past_peak_Nlag:=shift(app_past_peak,update_days,NA,"lag") ,by=.(Code_used_DS)] 
  dd_for_count_peaks[app_past_peak==app_past_peak_Nlag, app_past_peak_rest:=app_past_peak]
  dd_for_count_peaks[ , date_past_peak_Nlag:=shift(date_past_peak,update_days,NA,"lag") ,by=.(Code_used_DS)] 
  dd_for_count_peaks[date_past_peak==date_past_peak_Nlag, date_past_peak_rest:=date_past_peak]
  dd_for_count_peaks[is.na(date_past_peak_rest), app_past_peak_rest:=NA]
  
  dd_for_count_peaks[, app_past_peak_rest:= na.locf(app_past_peak_rest, na.rm = F), by=.(Code_used_DS)] 
  dd_for_count_peaks[, date_past_peak_rest := na.locf(date_past_peak_rest, na.rm = F), by=.(Code_used_DS)] 

  dd_for_count_peaks[,app_past_peak_Nlag:=NULL]
  dd_for_count_peaks[,date_past_peak_Nlag:=NULL]
  dd_for_count_peaks[,pass_peak_rest:=0]
  dd_for_count_peaks[appDS>app_past_peak_rest, pass_peak_rest:=1]
  dd_for_count_peaks[is.na(app_past_peak_rest), pass_peak_rest:=NA]
  dd_for_count_peaks[,app_yest:=NULL]
  
  count_peaks<-unique(dd_for_count_peaks[!is.na(date_past_peak_rest),.(Code_used_DS, date_past_peak_rest)])[,.N, by=.(Code_used_DS)]
  setnames(count_peaks, "N", "N_peaks")
  data_for_peaks<-merge(data_for_peaks, count_peaks[,.(N_peaks, 
                                                       Code_used_DS)], by=c("Code_used_DS"), all.x=T)
  
  data_for_peaks[N_peaks > data.table(sum_up(data_for_peaks[,.(N_peaks)], d=T) )$p50, countpeaks:="high"] 
  data_for_peaks[N_peaks <= data.table(sum_up(data_for_peaks[,.(N_peaks)], d=T)   )$p50, countpeaks:="low"]
  

  library(lfe)
  
  for(cond in 1:2){
    set=c("low", "high")

  summary(e21<- felm(excess21~ action + 0, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]]))
  summary(e21_diff<- felm(excess21~ action, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]]))
  
  summary(e120<- felm(excess120~ action + 0, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]]))
  summary(e120_diff<- felm(excess120~ action, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]]))
  
  summary(e260<- felm(excess260~ action + 0, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]]))
  summary(e260_diff<- felm(excess260~ action, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]] ))
  
  
  table<-stargazer( e21, e120,e260,
                    align=TRUE,no.space=TRUE, covariate.labels=c( "Av. Excess Return on Paper Losses","Av. Excess Return on Winning Stocks Sold"),
                    type="latex",
                    omit.table.layout = "n",
                    float=F, 
                    table.layout ="-dc#-tas-",
                    style = "aer",
                    column.sep.width = "10pt",
                    digits=4 , report = "vc"
  )
  
  
  table <- str_replace_all(table, "\\^", "")
  
  table2<-stargazer( e21_diff, e120_diff,  e260_diff,
                     align=TRUE,no.space=TRUE, covariate.labels=c("Difference in Excess Returns"),
                     type="latex",
                     omit.table.layout = "n",
                     float=F, 
                     table.layout ="-dc#-tas-",
                     style = "aer",
                     column.sep.width = "10pt",
                     digits=4 #, report = "vc"
  )
  
  table2 <- str_replace_all(table2, "\\^", "")
  
  setwd(wd_tables)
  write.table(c(table[c(11,10)],table2[10]), col.names = F, 
              row.names = F, quote = FALSE,  paste0("excess_returns_",set[cond],".tex")) 
 


  summary(e21<- felm(excess21~ action_peak + 0, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]]))
  summary(e21_diff<- felm(excess21~ action_peak, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]]))
  
  summary(e120<- felm(excess120~ action_peak + 0, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]]))
  summary(e120_diff<- felm(excess120~ action_peak, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]]))
  
  summary(e260<- felm(excess260~ action_peak + 0, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]]))
  summary(e260_diff<- felm(excess260~ action_peak, data_for_peaks[num_sales_the_day>0][countpeaks== set[cond]]))
  
  
  table<-stargazer( e21, e120,e260,
                    align=TRUE,no.space=TRUE, covariate.labels=c( "Av. Excess Return on Paper Losses","Av. Excess Return on Winning Stocks Sold"),
                    type="latex",
                    omit.table.layout = "n",
                    float=F, 
                    table.layout ="-dc#-tas-",
                    style = "aer",
                    column.sep.width = "10pt",
                    digits=4 , report = "vc"
  )
  
  table <- str_replace_all(table, "\\^", "")
  
  
  table2<-stargazer( e21_diff, e120_diff,  e260_diff,
                     align=TRUE,no.space=TRUE, covariate.labels=c("Difference in Excess Returns"),
                     type="latex",
                     omit.table.layout = "n",
                     float=F, 
                     table.layout ="-dc#-tas-",
                     style = "aer",
                     column.sep.width = "10pt",
                     digits=4 #, report = "vc"
  )
  
  table2 <- str_replace_all(table2, "\\^", "")
  
  setwd(wd_tables)
  write.table(c(table[c(11,10)],table2[10]), col.names = F, 
              row.names = F, quote = FALSE,  paste0("excess_returns_peak_",set[cond],".tex")) 
 


  av_returns<-data_for_peaks[countpeaks== set[cond]][num_sales_the_day>0][, mean(return.since.pur100), by=.(actions_DE)][order(actions_DE)] 
  av_returns[, actions_DE := fcase(
    actions_DE == "realized gains", "Realized Returns - Asset Above Purchase Price",
    actions_DE == "paper gains", "Unrealized Returns - Asset Above Purchase Price",
    actions_DE == "paper losses", "Unrealized Returns - Asset Below Purchase Price",
    actions_DE == "realized losses", "Realized Returns - Asset Below Purchase Price"
  )]
  
  av_returns[, V1:=round(V1,2)]
  
  
  options(xtable.sanitize.text.function=NULL)
  options(xtable.sanitize.colnames.function = identity)
  
  table <- capture.output(print(xtable(av_returns), include.rownames=FALSE)) 
  
  setwd(wd_tables)
  write.table(table[9:12], col.names = F, 
              row.names = F, quote = FALSE, paste0("av_returns_", set[cond],".tex"))
  
  
  av_returns<-data_for_peaks[countpeaks== set[cond]][num_sales_the_day>0][, mean(return.since.pur100), by=.(actions_DE_peak)][order(actions_DE_peak)]
  av_returns[, actions_DE_peak := fcase(
    actions_DE_peak == "realized gains", "Realized Returns - Asset Above Peak Price",
    actions_DE_peak == "paper gains", "Unrealized Returns - Asset Above Peak Price",
    actions_DE_peak == "paper losses", "Unrealized Returns - Asset Below Peak Price",
    actions_DE_peak == "realized losses", "Realized Returns - Asset Below Peak Price"
  )]
  
  av_returns[, V1:=round(V1,2)]
  table <- capture.output(print(xtable(av_returns), include.rownames=FALSE)) 
  
  setwd(wd_tables)
  write.table(table[9:12], col.names = F, 
              row.names = F, quote = FALSE, paste0("av_returns_peak_", set[cond],".tex"))
  
  
  }

  prop_peak<-data_for_peaks[,.N, by=.(countpeaks, gain.since.point,action_peak)][,total:=sum(N), by=.(countpeaks)][,pro:=N/total][order(countpeaks, gain.since.point)]
  prop_peak

  prop_pur<-data_for_peaks[,.N, by=.(countpeaks, gain.since.pur , action)][,total:=sum(N), by=.(countpeaks)][,pro:=N/total][order(countpeaks, gain.since.pur)]
  prop_pur
  }
  
  
# Part 3: Robustness Checks: splitting the sample into ten deciles based on returns since purchase.
# -------------------------

  if(data_name_read=="MAXC_peak_5update.csv" & use_days=="sell"){ 
    

    data_for_peaks[is.na(flag_no_peak),deciles_returns:=xtile(return.since.pur100,n=10)]

    for(dec in 1:10){
    summary(mdec<- felm(sell~  return.since.pur_pos100 + return.since.pur_neg100 +
                         return.since.point_pos100 + return.since.point_neg100  +gain.since.point +gain.since.pur                  | 0 | 0 | anon + port_date, 
                        data_for_peaks[deciles_returns==dec]))
      assign(  paste("m_d",  dec ,sep = "_"), mdec)

      mdec_s=summary(mdec)
      Coef_m=mdec_s$coefficients[,1]
      SE_m=mdec_s$coefficients[,2]
      SE_m[is.na(Coef_m)] <- NA   
      assign(  paste("SE_m",  dec ,sep = "_"),SE_m)

    }
    

    label_table= "Peak"
    label_table_long= "Gain Since Peak"
    
      names_of_IV<-  unique(names(unlist(lapply(list(m_d_1, m_d_2, m_d_3,  m_d_4, m_d_5, m_d_6, m_d_7, m_d_8, m_d_9, m_d_10), coef))))[-1]

      important_labels<- c(
        "Gain Since Purchase=1",
        "Return Since Purchase $>0$ $(\\%)$",
        "Return Since Purchase $<0$ $(\\%)$",
        paste0(label_table_long, "=1"),
        paste0("Returns Since ", label_table, " $>0$ $(\\%)$"),
        paste0("Returns Since ", label_table, " $<0$ $(\\%)$")
      )
      
      
      order_IV_omited<- c()
      models_list<-list(m_d_1, m_d_2, m_d_3,  m_d_4, m_d_5, m_d_6, m_d_7, m_d_8, m_d_9, m_d_10)
      SE_list<-list(SE_m_1,SE_m_2, SE_m_3, SE_m_4, SE_m_5, SE_m_6, SE_m_7, SE_m_8, SE_m_9, SE_m_10)
      
            table<-stargazer(models_list ,
                       se = SE_list,
                       title="", align=TRUE,type="latex",  
                       dep.var.labels=c(""),
                       order=c(6,1,2,5,3,4),
                       covariate.labels=important_labels,
                       omit        = NULL  ,         
                       omit.stat=c("LL","ser","f", "adj.rsq") , 
                       omit.table.layout = "n",
                       float=F, 
                       table.layout ="-dc#-tas-",
                       style = "aer",
                       column.sep.width = "-1pt",
                       digits=4
      ) 
      
      
      
      library(stringr)
      table <- str_replace_all(table, "\\^", "")
      table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
      setwd(wd_tables)
      write.table(table[10:(length(table)-2)], col.names = F, 
                  row.names = F, quote = FALSE, paste0("clean_OLS_deciles_", use_days ,"sample.tex"))
      

  }
  




if(data_name_read=="MAXC_peak_5update.csv" & use_days=="sell"){ 
  
  
  # Part 3: Robustness Checks: Tests investor responses to various gain measures
  # -------------------------
  
  dd <- fread("D:/files_moved_19/output datastream/datastream data/310518_prices/datastream_20180601.csv")    
  dd[,date:=as.Date(date, "%Y-%m-%d")]
  
  stocks<- unique(data_for_peaks$Code_used_DS)
  
  dd<-dd[Code_used_DS %in% stocks,.(app, date ,Code_used_DS )][order(Code_used_DS,date)]
  dd[ , app_lag1:=shift(app,1,NA,"lag") ,by=.(Code_used_DS)] 
  dd[ , app_lag5:=shift(app,5,NA,"lag") ,by=.(Code_used_DS)] 
  dd[ , app_lag20:=shift(app,20,NA,"lag") ,by=.(Code_used_DS)] 
  dd[ , app_lag60:=shift(app,60,NA,"lag") ,by=.(Code_used_DS)] 
  
  # Computing returns 
  
  dd[!is.na(app_lag1),return.since.lag1:=(app- app_lag1 )/app_lag1 ]
  dd[!is.na(app_lag1),return.since.lag1100:= return.since.lag1*100]
  dd[!is.na(app_lag1),return.since.lag1100_pos:= ifelse(return.since.lag1100>0, return.since.lag1100, 0)]
  dd[!is.na(app_lag1),return.since.lag1100_neg:= ifelse(return.since.lag1100<=0, return.since.lag1100, 0)]
  dd[!is.na(app_lag1),gain.since.lag1:=ifelse(return.since.lag1>0, 1, 0)]

  dd[!is.na(app_lag5),return.since.lag5:=(app- app_lag5 )/app_lag5 ]
  dd[!is.na(app_lag5),return.since.lag5100:= return.since.lag5*100]
  dd[!is.na(app_lag5),gain.since.lag5:=ifelse(return.since.lag5>0, 1, 0)]
  dd[!is.na(app_lag5),return.since.lag5100_pos:= ifelse(return.since.lag5100>0, return.since.lag5100, 0)]
  dd[!is.na(app_lag5),return.since.lag5100_neg:= ifelse(return.since.lag5100<=0, return.since.lag5100, 0)]
  
  dd[!is.na(app_lag20),return.since.lag20:=(app- app_lag20 )/app_lag20 ]
  dd[!is.na(app_lag20),return.since.lag20100:= return.since.lag20*100]
  dd[!is.na(app_lag20),gain.since.lag20:=ifelse(return.since.lag20>0, 1, 0)]
  dd[!is.na(app_lag20),return.since.lag20100_pos:= ifelse(return.since.lag20100>0, return.since.lag20100, 0)]
  dd[!is.na(app_lag20),return.since.lag20100_neg:= ifelse(return.since.lag20100<=0, return.since.lag20100, 0)]
  
  dd[!is.na(app_lag60),return.since.lag60:=(app- app_lag60 )/app_lag60 ]
  dd[!is.na(app_lag60),return.since.lag60100:= return.since.lag60*100]
  dd[!is.na(app_lag60),gain.since.lag60:=ifelse(return.since.lag60>0, 1, 0)]
  dd[!is.na(app_lag60),return.since.lag60100_pos:= ifelse(return.since.lag60100>0, return.since.lag60100, 0)]
  dd[!is.na(app_lag60),return.since.lag60100_neg:= ifelse(return.since.lag60100<=0, return.since.lag60100, 0)]
  dd[,.(return.since.lag1, return.since.lag1100, return.since.lag1100_pos, return.since.lag1100_neg)][1:20]
  dd[,.(return.since.lag5, return.since.lag5100, return.since.lag5100_pos, return.since.lag5100_neg)][1:20]
  dd[,.(return.since.lag20, return.since.lag20100, return.since.lag20100_pos, return.since.lag20100_neg)][1:90]
  dd[,.(return.since.lag60, return.since.lag60100, return.since.lag60100_pos, return.since.lag60100_neg)][1:90]
  
  dd[, year := year(date), by = .(Code_used_DS)]
  
  dd[, start_of_year_price := app[which.min(date)], by = .(Code_used_DS, year)]
  
  dd[, ytd_return := (app - start_of_year_price) / start_of_year_price]
  dd[, ytd_return_percent := ytd_return * 100]  
  dd[year == 2012, ytd_return_percent := NA]
  dd[!is.na(ytd_return_percent), gain_ytd := ifelse(ytd_return_percent >
                                                      0, 1, 0)]
  dd[!is.na(ytd_return_percent), return.ytd100_pos := ifelse(ytd_return_percent >
                                                               0, ytd_return_percent, 0)]
  dd[!is.na(ytd_return_percent), return.ytd100_neg := ifelse(ytd_return_percent <=
                                                               0, ytd_return_percent, 0)]
  

    
  dd[,port_date:=date]

  data_for_peaks<- merge(data_for_peaks, dd[,.(Code_used_DS, port_date, gain.since.lag1, gain.since.lag5, gain.since.lag20, gain.since.lag60, gain_ytd,
                                                 return.since.lag1100, return.since.lag1100_pos, return.since.lag1100_neg,
                                                 return.since.lag5100, return.since.lag5100_pos, return.since.lag5100_neg,
                                                 return.since.lag20100, return.since.lag20100_pos, return.since.lag20100_neg,
                                                 return.since.lag60100, return.since.lag60100_pos, return.since.lag60100_neg,
                                                 ytd_return_percent, return.ytd100_pos, return.ytd100_neg
                                                 )], 
                                     by=c("Code_used_DS", "port_date"), all.x=T)
  

  library(lfe)
   
    summary(mdec1<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                          gain.since.point    + return.since.point_pos100 + return.since.point_neg100  |
                           0 | 0 | anon + port_date, 
                        data_for_peaks))
    
    
    summary(mdec2<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                          gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                          gain.since.lag1 +  return.since.lag1100_pos + return.since.lag1100_neg| 0 | 0 | anon + port_date, 
                        data_for_peaks))
    
    summary(mdec3<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                          gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                          gain.since.lag5 +  return.since.lag5100_pos + return.since.lag5100_neg| 0 | 0 | anon + port_date, 
                        data_for_peaks))
    
    summary(mdec4<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                          gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                          gain.since.lag20 +  return.since.lag20100_pos + return.since.lag20100_neg| 0 | 0 | anon + port_date, 
                        data_for_peaks))
    

    summary(mdec5<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                          gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                          gain.since.lag60 +  return.since.lag60100_pos + return.since.lag60100_neg| 0 | 0 | anon + port_date, 
                        data_for_peaks))
    
    
    summary(mdec6<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                          gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                          gain_ytd +  return.ytd100_pos+ return.ytd100_neg| 0 | 0 | anon + port_date, 
                        data_for_peaks))

    summary(mdec7<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                          gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                          gain.since.lag1 +  return.since.lag1100_pos + return.since.lag1100_neg +
                          gain.since.lag5 +  return.since.lag5100_pos + return.since.lag5100_neg + 
                          gain.since.lag20 +  return.since.lag20100_pos + return.since.lag20100_neg + 
                          gain.since.lag60 +  return.since.lag60100_pos + return.since.lag60100_neg +
                          gain_ytd +  return.ytd100_pos+ return.ytd100_neg
                        | 0| 0 | anon + port_date, 
                        data_for_peaks))
    
    
    summary(mdec8<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                          gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                          gain.since.lag1 +  return.since.lag1100_pos + return.since.lag1100_neg +
                          gain.since.lag5 +  return.since.lag5100_pos + return.since.lag5100_neg + 
                          gain.since.lag20 +  return.since.lag20100_pos + return.since.lag20100_neg + 
                          gain.since.lag60 +  return.since.lag60100_pos + return.since.lag60100_neg +
                          gain_ytd +  return.ytd100_pos+ return.ytd100_neg
                          | anon| 0 | anon + port_date, 
                        data_for_peaks))
    
    

  important_labels_ORP<- c(
    "Gain Since Purchase=1",
    "Return Since Purchase $>0$ $(\\%)$",
    "Return Since Purchase $<0$ $(\\%)$",
    #
    "Gain Since Peak=1",
    "Return Since Peak $>0$ $(\\%)$",
    "Return Since Peak $<0$ $(\\%)$",  
    #
    "Gain Since Yesterday=1",
    "Return Since Yesterday $>0$ $(\\%)$",
    "Return Since Yesterday $<0$ $(\\%)$",  

    "Gain Since Past Week=1",
    "Return Since Past Week $>0$ $(\\%)$",
    "Return Since Past Week $<0$ $(\\%)$",  

    "Gain Since Past Month=1",
    "Return Since Past Month $>0$ $(\\%)$",
    "Return Since Past Month $<0$ $(\\%)$",  
    
    "Gain Since Past Quarter=1",
    "Return Since Past Quarter $>0$ $(\\%)$",
    "Return Since Past Quarter $<0$ $(\\%)$",  
    
    "Year-to-Date Gain=1",
    "Year-to-Date Return $>0$ $(\\%)$",
    "Year-to-Date Return $<0$ $(\\%)$"
  )
  
  table<-stargazer( mdec1, mdec2, mdec3, mdec4, mdec5, mdec6, mdec7, mdec8, 
                    no.space=TRUE,
                    align=TRUE,type="latex",
                    covariate.labels=important_labels_ORP,
                    omit        = NULL ,  
                    omit.stat=c("LL","ser","f", "adj.rsq") , 
                    omit.table.layout = "n",
                    float=F, 
                    table.layout ="-dc#-tas-",
                    style = "aer",
                    column.sep.width = "10pt",
                    add.lines=list(c("Account FE", "NO", "NO", "NO",  "NO", "NO", "NO", "NO", "YES")) ,
                    digits=4 
  )
  
  
  library(stringr)
  table <- str_replace_all(table, "\\^", "")
  table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")
  

  setwd(wd_tables)
  write.table(table[10:(length(table)-2)], col.names = F, 
              row.names = F, quote = FALSE,   "controling_alternative_rp_sell_sample.tex") 
  
  
  
  # Part 4: Timing of Peaks
  # -----------------------  
  

data_for_peaks[,peak_in_past_1month:=NULL]
data_for_peaks[,peak_in_current_month:=NULL]
data_for_peaks[,peak_in_past_5Mmonth:=NULL]
data_for_peaks[,peak_in_past_1Mmonth:=NULL]
data_for_peaks[,peak_in_past_4month:=NULL]
data_for_peaks[,peak_in_past_3month:=NULL]
data_for_peaks[,peak_in_past_2month:=NULL]

data_for_peaks[(format(date_past_point, "%Y-%m") <= format(port_date %m-% months(5), "%Y-%m")), peak_in_past_5Mmonth :=  1]
data_for_peaks[(format(date_past_point, "%Y-%m") == format(port_date %m-% months(4), "%Y-%m")), peak_in_past_4month :=  1]
data_for_peaks[(format(date_past_point, "%Y-%m") == format(port_date %m-% months(3), "%Y-%m")), peak_in_past_3month :=  1]
data_for_peaks[(format(date_past_point, "%Y-%m") == format(port_date %m-% months(2), "%Y-%m")), peak_in_past_2month :=  1]
data_for_peaks[(format(date_past_point, "%Y-%m") == format(port_date %m-% months(1), "%Y-%m")), peak_in_past_1month :=  1]
data_for_peaks[(format(date_past_point, "%Y-%m") == format(port_date %m-% months(0), "%Y-%m")), peak_in_current_month :=  1]
data_for_peaks[(format(date_past_point, "%Y-%m") <= format(port_date %m-% months(1), "%Y-%m")), peak_in_past_1Mmonth :=  1]




summary(mdec1<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                       gain.since.point    + return.since.point_pos100 + return.since.point_neg100  |
                       0 | 0 | anon + port_date,
                     data_for_peaks[peak_in_past_5Mmonth==1]))


summary(mdec2<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                       gain.since.point    + return.since.point_pos100 + return.since.point_neg100  |
                       0 | 0 | anon + port_date,
                     data_for_peaks[peak_in_past_4month==1]))

summary(mdec3<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                       gain.since.point    + return.since.point_pos100 + return.since.point_neg100  |
                       0 | 0 | anon + port_date,
                     data_for_peaks[peak_in_past_3month==1]))


summary(mdec4<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                       gain.since.point    + return.since.point_pos100 + return.since.point_neg100  |
                       0 | 0 | anon + port_date,
                     data_for_peaks[peak_in_past_2month==1]))

summary(mdec5<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                       gain.since.point    + return.since.point_pos100 + return.since.point_neg100  |
                       0 | 0 | anon + port_date , 
                     data_for_peaks[peak_in_past_1month==1]))



summary(mdec6<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                       gain.since.point    + return.since.point_pos100 + return.since.point_neg100  |
                       0 | 0 | anon + port_date , 
                     data_for_peaks[peak_in_current_month==1]))


summary(mdec1L<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                       gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                       gain.since.lag1 +  return.since.lag1100_pos + return.since.lag1100_neg +
                       gain.since.lag5 +  return.since.lag5100_pos + return.since.lag5100_neg + 
                       gain.since.lag20 +  return.since.lag20100_pos + return.since.lag20100_neg + 
                       gain.since.lag60 +  return.since.lag60100_pos + return.since.lag60100_neg +
                       gain_ytd +  return.ytd100_pos+ return.ytd100_neg
                     | anon| 0 | anon + port_date, 
                     data_for_peaks[peak_in_past_5Mmonth==1]))


summary(mdec2L<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                       gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                       gain.since.lag1 +  return.since.lag1100_pos + return.since.lag1100_neg +
                       gain.since.lag5 +  return.since.lag5100_pos + return.since.lag5100_neg + 
                       gain.since.lag20 +  return.since.lag20100_pos + return.since.lag20100_neg + 
                       gain.since.lag60 +  return.since.lag60100_pos + return.since.lag60100_neg +
                       gain_ytd +  return.ytd100_pos+ return.ytd100_neg
                     | anon| 0 | anon + port_date, 
                     data_for_peaks[peak_in_past_4month==1]))


summary(mdec3L<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                       gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                       gain.since.lag1 +  return.since.lag1100_pos + return.since.lag1100_neg +
                       gain.since.lag5 +  return.since.lag5100_pos + return.since.lag5100_neg + 
                       gain.since.lag20 +  return.since.lag20100_pos + return.since.lag20100_neg + 
                       gain.since.lag60 +  return.since.lag60100_pos + return.since.lag60100_neg +
                       gain_ytd +  return.ytd100_pos+ return.ytd100_neg
                     | anon| 0 | anon + port_date, 
                     data_for_peaks[peak_in_past_3month==1]))

summary(mdec4L<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                       gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                       gain.since.lag1 +  return.since.lag1100_pos + return.since.lag1100_neg +
                       gain.since.lag5 +  return.since.lag5100_pos + return.since.lag5100_neg + 
                       gain.since.lag20 +  return.since.lag20100_pos + return.since.lag20100_neg + 
                       gain.since.lag60 +  return.since.lag60100_pos + return.since.lag60100_neg +
                       gain_ytd +  return.ytd100_pos+ return.ytd100_neg
                     | anon| 0 | anon + port_date, 
                     data_for_peaks[peak_in_past_2month==1]))



summary(mdec5L<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                       gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                       gain.since.lag1 +  return.since.lag1100_pos + return.since.lag1100_neg +
                       gain.since.lag5 +  return.since.lag5100_pos + return.since.lag5100_neg + 
                       gain.since.lag20 +  return.since.lag20100_pos + return.since.lag20100_neg + 
                       gain.since.lag60 +  return.since.lag60100_pos + return.since.lag60100_neg +
                       gain_ytd +  return.ytd100_pos+ return.ytd100_neg
                     | anon| 0 | anon + port_date, 
                     data_for_peaks[peak_in_past_1month==1]))



summary(mdec6L<- felm(sell~ gain.since.pur  + return.since.pur_pos100 + return.since.pur_neg100 +
                        gain.since.point    + return.since.point_pos100 + return.since.point_neg100  +
                        gain.since.lag1 +  return.since.lag1100_pos + return.since.lag1100_neg +
                        gain.since.lag5 +  return.since.lag5100_pos + return.since.lag5100_neg + 
                        gain.since.lag20 +  return.since.lag20100_pos + return.since.lag20100_neg + 
                        gain.since.lag60 +  return.since.lag60100_pos + return.since.lag60100_neg +
                        gain_ytd +  return.ytd100_pos+ return.ytd100_neg
                      | anon| 0 | anon + port_date, 
                      data_for_peaks[peak_in_current_month==1]))



models<-list(mdec6, mdec6L, mdec5, mdec5L, mdec4,mdec4L,  mdec3, mdec3L, mdec2, mdec2L, mdec1, mdec1L)
table<-stargazer(models,
                  no.space=TRUE,
                  align=TRUE,type="latex",
                  covariate.labels=important_labels_ORP,
                  omit        = NULL ,  
                  omit.stat=c("LL","ser","f", "adj.rsq") , 
                  omit.table.layout = "n",
                  float=F, 
                  table.layout ="-dc#-tas-",
                  style = "aer",
                  column.sep.width = "10pt",
                  add.lines=list(c("Account FE", "NO", "YES", "NO",  "YES", "NO", "YES", "NO", "YES", "NO", "YES", "NO", "YES")) ,
                  digits=4 
)


library(stringr)
table <- str_replace_all(table, "\\^", "")
table <- str_replace_all(table, "R\\$\\{2\\}\\$", "R$^{2}$")


setwd(wd_tables)
write.table(table[10:(length(table)-2)], col.names = F, 
            row.names = F, quote = FALSE,   "peak_timing_wide_sell_sample.tex") 


}